package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampCustBizInfo;
import com.telecom.cms.util.Constant;

public class CampCustBizInfoDAO {

	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampCustBizInfo transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db
				+ ".Camp_Cust_Biz_Info where Cust_Id='"
				+ transientInstance.getCustId()+"' and prd_inst_id='"
				+ transientInstance.getPrdInstId()+"'";
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Cust_Biz_Info set latn_id='"
						+ transientInstance.getLatnId() + "',cust_id='"
						+ transientInstance.getCustId() + "',prd_inst_id='"
						+ transientInstance.getPrdInstId() + "',accs_nbr='"
						+ transientInstance.getAccsNbr() + "',all_accs_nbr='"
						+ transientInstance.getAllAccsNbr() + "',linkman='"
						+ transientInstance.getLinkman() + "',relationship='"
						+ transientInstance.getRelationship() + "',cont_Fix='"
						+ transientInstance.getContFix() + "',cont_Fax='"
						+ transientInstance.getContFax() + "',cont_Phs='"
						+ transientInstance.getContPhs() + "',Comp_Name_1='"
						+ transientInstance.getCompName1() + "',Cont_Nbr_1='"
						+ transientInstance.getContNbr1() + "',Comp_Name_2='"
						+ transientInstance.getCompName2() + "',Cont_Nbr_2='"
						+ transientInstance.getContNbr2() + "',Cont_Email='"
						+ transientInstance.getContEmail() + "',Cont_Uc='"
						+ transientInstance.getContUc() + "',Send_Addr='"
						+ transientInstance.getSendAddr() + "',Remark='"
						+ transientInstance.getRemark() + "' where Cust_Id='"
						+ transientInstance.getCustId()+"' and prd_inst_id='"
							+ transientInstance.getPrdInstId()+"'";
			else
				sql = "insert into " + db + ".Camp_Cust_Biz_Info values('"
						+ transientInstance.getLatnId() + "','"
						+ transientInstance.getCustId() + "','"
						+ transientInstance.getPrdInstId() + "','"
						+ transientInstance.getAccsNbr() + "','"
						+ transientInstance.getAllAccsNbr() + "','"
						+ transientInstance.getLinkman() + "','"
						+ transientInstance.getRelationship() + "','"
						+ transientInstance.getContFix() + "','"
						+ transientInstance.getContFax() + "','"
						+ transientInstance.getContPhs() + "','"
						+ transientInstance.getCompName1() + "','"
						+ transientInstance.getContNbr1() + "','"
						+ transientInstance.getCompName2() + "','"
						+ transientInstance.getContNbr2() + "','"
						+ transientInstance.getContEmail() + "','"
						+ transientInstance.getContUc() + "','"
						+ transientInstance.getSendAddr() + "','"
						+ transientInstance.getRemark() + "')";
			rs = dao.insert(sql);
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		} finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampCustBizInfo persistentInstance) {

		String sql = "delete from " + db
				+ ".Camp_Cust_Biz_Info where Cust_Id = "
				+ persistentInstance.getCustId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampCustBizInfo findById(String custid,String prdinstid) {

		String sql = "select * from " + db
				+ ".Camp_Cust_Biz_Info where Cust_Id = '" + custid+"' and prd_inst_id='"+prdinstid+"'";
		CampCustBizInfo sd = new CampCustBizInfo();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setLatnId(rs.getString(1));
				sd.setCustId(rs.getString(2));
				sd.setPrdInstId(rs.getString(3));
				sd.setAccsNbr(rs.getString(4));
				sd.setAllAccsNbr(rs.getString(5));
				sd.setLinkman(rs.getString(6));
				sd.setRelationship(rs.getString(7));
				sd.setContFix(rs.getString(8));
				sd.setContFax(rs.getString(9));
				sd.setContPhs(rs.getString(10));
				sd.setCompName1(rs.getString(11));
				sd.setContNbr1(rs.getString(12));
				sd.setCompName2(rs.getString(13));
				sd.setContNbr2(rs.getString(14));
				sd.setContEmail(rs.getString(15));
				sd.setContUc(rs.getString(16));
				sd.setSendAddr(rs.getString(17));
				sd.setRemark(rs.getString(18));

			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select * from " + db + ".Camp_Cust_Biz_Info";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCustBizInfo sd = new CampCustBizInfo();
				sd.setLatnId(rs.getString(1));
				sd.setCustId(rs.getString(2));
				sd.setPrdInstId(rs.getString(3));
				sd.setAccsNbr(rs.getString(4));
				sd.setAllAccsNbr(rs.getString(5));
				sd.setLinkman(rs.getString(6));
				sd.setRelationship(rs.getString(7));
				sd.setContFix(rs.getString(8));
				sd.setContFax(rs.getString(9));
				sd.setContPhs(rs.getString(10));
				sd.setCompName1(rs.getString(11));
				sd.setContNbr1(rs.getString(12));
				sd.setCompName2(rs.getString(13));
				sd.setContNbr2(rs.getString(14));
				sd.setContUc(rs.getString(15));
				sd.setSendAddr(rs.getString(16));
				sd.setRemark(rs.getString(17));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
